#include "project.h"

static LOG_HANDLE log_handle;


int init_errlog(char *program, char *errlog_path)
{
    if(errlog_path)
    {
        char dirname[LONG_FILE_NM_SIZE+1];
        char basename[FILE_NM_SIZE+1];
        char log_file[LONG_FILE_NM_SIZE+1];
        char buf[LONG_FILE_NM_SIZE+1];

        if(!program)  return(-1);
        extract_dirname(program, dirname);
        extract_basename(program, basename);
        CLR_BUF(log_file);
        if(errlog_path[0] != '/')
            strcpy(log_file, dirname);
        strcat(log_file, errlog_path);
        sprintf(buf, "/%s.log", basename);
        strcat(log_file, buf);
        log_handle=log_init(log_file, 0);
    }
    else
        log_handle=log_init("stdout", 0);
    return(log_handle == NULL);
}


int errlog(char *fmt, ...)
{
    char curr_ts[TIMESTAMP_COMMON_SIZE+1];
    va_list arglist;
    char log_buf[BUFFER_SIZE];
    int len;

    va_start(arglist, fmt);
    timestamp_fmt(curr_timestamp_str(curr_ts), NULL, curr_ts, NULL);
    len=sprintf(log_buf, "%s -- ", curr_ts);
    vsnprintf(log_buf+len, sizeof(log_buf)-len-1, fmt, arglist);
    va_end(arglist);
    strcat(log_buf, "\n");
    return(log_write(log_handle, log_buf)); 
}
